import EventPackage.*;

/**
 * @author Kyle Benedict
 */
class SprinklerController extends Controller {

    private static final long DELAY_BETWEEN_EVENTS_MILLIS = 1000L;

    public SprinklerController(String eventManagerIPAddress) {
        super(eventManagerIPAddress, DELAY_BETWEEN_EVENTS_MILLIS);
    }

    @Override
    protected EventType[] getEventTypesToRegister() {
        return new EventType[]{
            EventType.SPRINKLER_OFF,
            EventType.SPRINKLER_ON
        };
    }

    @Override
    protected int getHeartbeatEventId() {
        return 112;
    }

    @Override
    protected void onEvent(Event event) {
        if (EventType.SPRINKLER_OFF.isEvent(event.GetEventId(), event.GetMessage())) {
            setEnabled(false);
            setIndicatorMessageAndColor(EventType.SPRINKLER_OFF.getIndicatorMessage(), 0);
        } else if (EventType.SPRINKLER_ON.isEvent(event.GetEventId(), event.GetMessage())) {
            setEnabled(true);
            setIndicatorMessageAndColor(EventType.SPRINKLER_ON.getIndicatorMessage(), 1);
        }
    }

    public static void main(String args[]) {
        Controller controller = null;
        if (args.length == 0) {
            // event manager is on the local system
            controller = new SprinklerController(null);
        } else {
            controller = new SprinklerController(args[0]);
        }
        try {
            controller.processEvents();
        } catch (Exception e) {
            System.out.println(String.format("Error processing events: ", e));
            e.printStackTrace();
        }
    }

    @Override
    protected void onRegistered() {
        super.onRegistered();
        indicator = createIndicator(EventType.SPRINKLER_OFF.getIndicatorMessage(), 0.05F, 0.05F, 0);
        setIndicator(indicator);        
    }

    @Override
    protected void onUnregistered() {
        super.onUnregistered();
        if (indicator != null) {
            indicator.dispose();
        }
    }

}